package com.js.nickcar.module.addcar.valuation;

import com.js.nickcar.api.HttpHelpter;
import com.js.nickcar.api.UrlConfig;
import com.js.nickcar.api.UrlUtils;
import com.js.nickcar.base.ApiCallback;
import com.js.nickcar.base.BasePresenter;
import com.js.nickcar.base.HttpResultFunc;
import com.js.nickcar.entity.remote.CarInfoBean;
import com.zyzd.commontools.log.TLog;

import java.util.Map;

import rx.Observable;
import rx.Subscriber;

/**
 * Created by LZY on 2017/10/10.
 */
public class ValuationEntryPresenter extends BasePresenter<ValuationEntryView> {
    public String mCarId;

    public void setCarId(String carId) {
        mCarId = carId;
    }

    public void commitCarPrice(String valuationEntry, String additionalRemarks) {
        Map<String, String> requestMap = UrlUtils.getBaseRequestMap(UrlConfig.API_APPRAISER_CAR_PRICE);
        requestMap.put("car_id", mCarId);
        requestMap.put("price", valuationEntry);
        requestMap.put("description", additionalRemarks);
        Observable observable = HttpHelpter.getApiService().commitCarPrice(requestMap).map(new HttpResultFunc());
        addSubscription(observable, getCommitCarPriceSubscriber());
    }

    private Subscriber getCommitCarPriceSubscriber() {
        return new ApiCallback() {
            @Override
            public void onSuccess(Object model) {
                mView.onCommitCarPriceSuccess();
            }

            @Override
            public void onFailure(String msg) {
                showToast(msg);
            }

            @Override
            public void onFinish() {
            }
        };
    }

    public void getCarInfo() {
        Map<String, String> requestMap = UrlUtils.getBaseRequestMap(UrlConfig.API_APPRAISER_CAR_INFO);
        requestMap.put("car_id", mCarId);
        TLog.d("zyzd", "ValuationEntryPresenter>>getCarInfo--> " + mCarId);
        Observable<CarInfoBean> observable = HttpHelpter.getApiService().getCarInfo(requestMap).map(new HttpResultFunc<CarInfoBean>());
        addSubscription(observable, getCarInfoSubscriber());
    }

    private Subscriber<CarInfoBean> getCarInfoSubscriber() {
        return new ApiCallback<CarInfoBean>() {
            @Override
            public void onSuccess(CarInfoBean carInfoBean) {
                TLog.d("zyzd", "ValuationEntryPresenter>>onSuccess--> " + carInfoBean);
                mView.getCarInfoSuccess(carInfoBean);
            }

            @Override
            public void onFailure(String msg) {
                TLog.d("zyzd", "ValuationEntryPresenter>>onFailure--> " + msg);
            }

            @Override
            public void onFinish() {
                TLog.d("zyzd", "ValuationEntryPresenter>>onFinish--> ");
            }
        };
    }


}
